package com.fgbf.todo.Activities;

import java.io.IOException;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;

import com.fgbf.todo.R;
import com.fgbf.todo.MainClasses.LocItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Class that displays the map with points inside an acitivity
 * @author Warren Shenk
 */
public class LocationActivity extends MapActivity {
	
	////////////////////////////////////////////////////////////////////////
	// INSTANCE DATA
	////////////////////////////////////////////////////////////////////////

	/**
	 * The mapview of the activity
	 */
	MapView mapView;
	
	/**
	 * The geocoder
	 */
	Geocoder gc;
	
	/**
	 * list of addresses
	 */
	List<Address> addrList;

	
	////////////////////////////////////////////////////////////////////////
	// METHODS
	////////////////////////////////////////////////////////////////////////
	
	/**
	 * Called when the activity is created
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map_screen);
		
		gc = new Geocoder(this);
		GeoPoint pt = new GeoPoint(0,0);
		
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		
		Bundle extras = getIntent().getExtras();
		String locName = extras.getString("Location");
		
		Log.v("Location: ", locName);
		
		try {
			addrList = gc.getFromLocationName(locName, 1);
			
			
			if(addrList!=null && addrList.size()>0) {
		        int lat = (int)(addrList.get(0).getLatitude()*1000000);
		        int lng = (int)(addrList.get(0).getLongitude()*1000000);
		        pt = new GeoPoint(lat,lng);
		        mapView.getController().setZoom(15);
		        mapView.getController().setCenter(pt);
		      }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		LocItemizedOverlay itemizedoverlay = new LocItemizedOverlay(drawable, this);
		
		OverlayItem overlayitem = new OverlayItem(pt, "", locName);
		
		itemizedoverlay.addOverlay(overlayitem);
		mapOverlays.add(itemizedoverlay);

	}
	//-------------------------------------------------------------------------------------
	
	/**
	 * Called when the back button is pressed 
	 */
	public void onBackPressed() {
		LocationActivity.this.finish();
	}
	//-------------------------------------------------------------------------------------

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	//-------------------------------------------------------------------------------------


}
